package com.fudan.cfg.base;

import java.lang.reflect.InvocationTargetException;
import java.util.Map;

import com.fudan.cfg.trans.CombineNode;
import com.fudan.cfg.trans.TestNode;
import com.fudan.run.ctx.TransContext;

public abstract class TransDef extends TypeNode{

	public TransDef(String name, String type,String target) {
		super(name, type,target);
	}

	public static TransDef buildFromConfiguration(Map<String,Object> configuration,TransContext transContext) {
		String name = (String) configuration.get(NAME_TAG);
		String type = (String) configuration.get(TYPE_TAG);
		String target = (String) configuration.get(TARGET_TAG);
		if(name == null) {
			name = target;
		}
		Class<? extends TransDef> parser = transContext.getParser(type);
		if(parser!=null) {
			try {
				return parser.getConstructor(String.class,String.class,String.class).newInstance(name,type,target).build(configuration);
			} catch (InstantiationException | IllegalAccessException | IllegalArgumentException
					| InvocationTargetException | NoSuchMethodException | SecurityException e) {
				throw new RuntimeException(String.format("trans构建配置解析器出错，type: %s",type),e);
			}
		}
		throw new RuntimeException(String.format("trans没有匹配的配置解析器，type: %s",type));
	}

	public abstract TransDef build(Map<String,Object> configuration);
}
